import 'package:flutter/material.dart';
import 'package:hello_word/l10n/localization_intl.dart';
import 'package:hello_word/pages/home_page.dart';
import 'package:hello_word/pages/project_page.dart';
import 'package:hello_word/pages/structure_page.dart';
import 'package:hello_word/pages/user_page.dart';
import 'package:hello_word/pages/wechat_account_page.dart';

List<Widget> pages = <Widget>[
  HomePage(),
  ProjectPage(),
  WechatAccountPage(),
  StructurePage(),
  UserPage()
];

class TabNavigator extends StatefulWidget {
  TabNavigator({Key key}) : super(key: key);

  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  PageController _controller;
  int _selectedIndex = 1;
  DateTime _lastPressTime;

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: _selectedIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async {
          if (_lastPressTime == null ||
              DateTime.now().difference(_lastPressTime) >
                  Duration(seconds: 1)) {
            _lastPressTime = DateTime.now();
            return false;
          }
          return true;
        },
        child: PageView.builder(
          itemBuilder: (context, index) => pages[index],
          itemCount: pages.length,
          controller: _controller,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: (value) {
            setState(() {
              _selectedIndex = value;
            });
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _selectedIndex,
        fixedColor: Colors.brown,
        unselectedItemColor: Colors.grey,
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text(MyLocalizations.of(context).tabHome)),
          BottomNavigationBarItem(
              icon: Icon(Icons.format_list_bulleted),
              title: Text(MyLocalizations.of(context).tabProject)),
          BottomNavigationBarItem(
              icon: Icon(Icons.group_work),
              title: Text(MyLocalizations.of(context).tabWechatAccount)),
          BottomNavigationBarItem(
              icon: Icon(Icons.call_split),
              title: Text(MyLocalizations.of(context).tabStructure)),
          BottomNavigationBarItem(
              icon: Icon(Icons.insert_emoticon),
              title: Text(MyLocalizations.of(context).tabUser)),
        ],
        onTap: (value) {
          _controller.jumpToPage(value);
        },
      ),
    );
  }
}
